package xyz.zhangyi.onjava.java8.example.lambda.combinator;

import java.math.BigDecimal;
import java.util.function.Function;

public class OrderService {
    public Order calculate(Order order) {
        Function<Order, Order> calculateTotalPrice = (o) -> {
            BigDecimal total = BigDecimal.ZERO;
            for (OrderItem orderItem : o.getOrderItems()) {
                total.add(orderItem.subTotalPrice());
            }
            return o.copyFromTotalPrice(total);
        };

        Function<Order, Order> calculateDiscount = (o) -> {
            return o.copyFromDiscount(o.getTotalPrice().multiply(BigDecimal.valueOf(0.1)));
        };

        Function<Order, Order> calculateShippingFee = (o) -> {
            return o.copyFromShippingFee(o.getTotalPrice().multiply(BigDecimal.valueOf(0.05)));
        };

        return calculateTotalPrice.andThen(calculateDiscount.andThen(calculateShippingFee)).apply(order);
    }
}
